﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoUser.Data.Models;
using System.Data.Entity.Migrations;

namespace TodoUser.Data
{
    public class Seeder
    {
        // Seed method
        public static void Seed(ApplicationDbContext db, bool users = true, bool todos = true, bool roles = true)
        {
            if (roles)
            {
                SeedRoles(db);
            }
            if (users)
            {
                SeedUser(db);
            }
            if (todos)
            {
                SeedTodo(db);
            }

        }

        private static void SeedRoles(ApplicationDbContext db)
        {


            var store = new RoleStore<IdentityRole>(db);
            var manager = new RoleManager<IdentityRole>(store);
            if (!manager.RoleExists("User"))
            {
                manager.Create(new IdentityRole { Name = "User" });
            }
            if (!manager.RoleExists("Admin"))
            {
                manager.Create(new IdentityRole { Name = "Admin" });
            }

        }

        private static void SeedUser(ApplicationDbContext db)
        {
            var store = new UserStore<ApplicationUser>(db);
            var manager = new UserManager<ApplicationUser>(store);

            // check if user MIKE exists..
            if (!db.Users.Any(a => a.UserName == "Mike"))
            {
                ApplicationUser user = new ApplicationUser
                {
                    UserName = "Mike",
                    Email = "Mike@codercamps",
                    EmailConfirmed = true
                };
                manager.Create(user, "123456");
                manager.AddToRole(user.Id, "User");
                db.SaveChanges();
            }
            // check if user SUSAN exists..
            if (!db.Users.Any(a => a.UserName == "Susan"))
            {
                ApplicationUser user = new ApplicationUser
                {
                    UserName = "Susan",
                    Email = "Susan@codercamps",
                    EmailConfirmed = true
                };
                manager.Create(user, "123456");
                manager.AddToRole(user.Id, "User");
                db.SaveChanges();
            }
            // check if Admin exists..
            if (!db.Users.Any(a => a.UserName == "Administrator"))
            {
                ApplicationUser user = new ApplicationUser
                {
                    UserName = "Administrator",
                    Email = "Administrator@codercamps",
                    EmailConfirmed = true
                };
                manager.Create(user, "123456");
                manager.AddToRole(user.Id, "Admin");
                db.SaveChanges();
            }
        }


        private static void SeedTodo(ApplicationDbContext db)
        {
            //get users from DB
            string user1Id = db.Users.SingleOrDefault(u => u.UserName == "Mike").Id;
            string user2Id = db.Users.SingleOrDefault(u => u.UserName == "Susan").Id;
            // Seed the TODOS
            db.Todos.AddOrUpdate(t => t.TodoId,
                new Todo() { Task = "Dishes", UserId = user1Id, TimeAdded = DateTime.Now },
                new Todo() { Task = "Carwash", UserId = user1Id, TimeAdded = DateTime.Now },
                new Todo() { Task = "Walk dog", UserId = user2Id, TimeAdded = DateTime.Now },
                new Todo() { Task = "Get Milk", UserId = user2Id, TimeAdded = DateTime.Today.AddDays(-5), TimeCompleted = DateTime.Now },
                new Todo() { Task = "Get Eggs", UserId = user2Id, TimeAdded = DateTime.Now }
                );
            db.SaveChanges();

        }
    }
}
